package com.qingge.springboot.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingge.springboot.common.Constants;
import com.qingge.springboot.common.Result;
import com.qingge.springboot.entity.Dict;
import com.qingge.springboot.entity.Menu;
import com.qingge.springboot.mapper.DictMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.qingge.springboot.service.IMenusService;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 青哥哥
 * @since 2024-04-12
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private IMenusService menusService;

    @Resource
    private DictMapper dictMapper;

    // 新增和修改
    @PostMapping
    //@RequestBody 把前台json对象转化为java对象
    // 新增或者更新
    public Result save(@RequestBody Menu menu){
        menusService.saveOrUpdate(menu);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        menusService.removeById(id);
        return Result.success();
    }

    //批量删除接口
    @PostMapping("/del/batch") //这个id和下面的id一一对应
    public Result deleteBatch(@RequestBody List<Integer> ids){
        menusService.removeByIds(ids); //请求参数是变量，用RequestBody注解
        return Result.success();
    }

//    @GetMapping("/ids")
//    public Result findAllIds() {
//        // 查出sys_menus表所有的id
//        return Result.success(menusService.list().stream().map(Menu::getId));
//    }

    @GetMapping
    public Result findAll(@RequestParam(defaultValue = "") String name) {
        return Result.success(menusService.findMenus(name));
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
            return Result.success(menusService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam String name) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
        queryWrapper.orderByDesc("id");
        return Result.success(menusService.page(new Page<>(pageNum, pageSize),queryWrapper));
    }

    @GetMapping("/icons")
    public Result getIcons() {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", Constants.DICT_TYPE_ICON);
        return Result.success(dictMapper.selectList(null));
    }

}

